using System.IO;

namespace FileTransferDesktop.Models;

public class UploadFileItem
{
    public string FilePath { get; }
    public string FileName { get; }
    public double FileSize { get; }
    public string FileSizeFormatted { get; }

    public UploadFileItem(string filePath)
    {
        FilePath = filePath;
        FileName = Path.GetFileName(filePath);
        var fileInfo = new FileInfo(filePath);
        FileSize = fileInfo.Length;
        FileSizeFormatted = FormatFileSize(FileSize);
    }

    private static string FormatFileSize(double bytes)
    {
        string[] sizes = { "B", "KB", "MB", "GB", "TB" };
        int order = 0;
        while (bytes >= 1024 && order < sizes.Length - 1)
        {
            order++;
            bytes = bytes / 1024;
        }
        return $"{bytes:0.##} {sizes[order]}";
    }
}
